package leetcodecompetition.D356;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author: Siantar
 * @Date: 2023-07-30-10:34
 * @Description: 1.0
 */
public class T6900 {
    public int countCompleteSubarrays(int[] nums) {
        int n = nums.length;
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int sum = set.size();
        int res = 0;
        Map<Integer, Integer> map = new HashMap<>();
        int left = 0, right = 0;
        while (right < n) {
            while (right < n && map.size() < sum) {
                map.put(nums[right], map.getOrDefault(nums[right], 0) + 1);
                right++;
            }
            while (map.size() >= sum) {
                res+=n-right;
                int d = map.getOrDefault(nums[left], 0);
                if (d == 1) {
                    map.remove(nums[left]);
                } else {
                    map.put(nums[left], d - 1);
                }
                left++;
            }
        }
        return res;
    }
    public int countCompleteSubarrays0(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            HashSet<Integer> curr = new HashSet<>();
            for (int j = i; j < nums.length; j++) {
                curr.add(nums[j]);
                count += curr.size() / set.size();
            }
        }
        return count;
    }
}
